﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SimplifyLauncher
{
    public partial class AddShortcutForm : Form
    {
        Launcher launcher;
        SettingsForm settingsForm;
        Rectangle iconRectangle;
        Pen pen;
        Font boldFont;
        int iconWidth, iconHeight, paddingX, separatorY;

        const int PADDING = 10;
        const int BOX_WIDTH = 190;
        const int PADDING_PANEL = PADDING - 2;
        const int MIDDLE_LEFT = PADDING * 4 + BOX_WIDTH;

        public AddShortcutForm(Launcher launcher, SettingsForm settingsForm)
        {
            this.launcher = launcher;
            this.settingsForm = settingsForm;
            this.iconWidth = launcher.IconWidth;
            this.iconHeight = launcher.IconHeight;

            this.AutoSize = true;
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            this.BackColor = Color.FromArgb(25, 25, 25);
            this.FormBorderStyle = FormBorderStyle.None;
            //this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MinimizeBox = false;
            this.MaximizeBox = false;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.Text = "Add Shortcut";

            pen = new Pen(SystemColors.ControlDark, 1);
            FontFamily calibri = new FontFamily("Calibri");
            //boldFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Bold);
            this.Font = new Font(calibri, 9, FontStyle.Regular);
            boldFont = new Font(calibri, 9, FontStyle.Bold);

            Label pathLabel = new Label() { Left = PADDING, Top = PADDING, AutoSize = true, Text = "Path to File/Folder" };
            TextBox pathBox = new TextBox() { Left = PADDING, Top = pathLabel.Bottom, Width = BOX_WIDTH };
            Button pathBrowse = new Button() { Top = pathBox.Bottom + PADDING_PANEL / 2, Text = "Browse" };
            pathBrowse.Left = pathBox.Right - pathBrowse.Width + 1;
            pathBrowse.Click += new EventHandler(PathBrowse_Click);
            this.Controls.Add(pathLabel);
            this.Controls.Add(pathBox);
            this.Controls.Add(pathBrowse);

            Label argumentsLabel = new Label() { Left = PADDING, Top = pathBrowse.Bottom + PADDING, AutoSize = true, Text = "Arguments" };
            TextBox argumentsBox = new TextBox() { Left = PADDING, Top = argumentsLabel.Bottom, Width = BOX_WIDTH };
            this.Controls.Add(argumentsLabel);
            this.Controls.Add(argumentsBox);

            Label labelLabel = new Label() { Left = PADDING, Top = argumentsBox.Bottom + PADDING, AutoSize = true, Text = "Label" };
            TextBox labelBox = new TextBox() { Left = PADDING, Top = labelLabel.Bottom, Width = BOX_WIDTH };
            this.Controls.Add(labelLabel);
            this.Controls.Add(labelBox);

            Label iconLabel = new Label() { Left = MIDDLE_LEFT, Top = PADDING, AutoSize = true, Text = "Icon" };
            Label iconSizeLabel = new Label() { Left = MIDDLE_LEFT + iconLabel.PreferredWidth, Top = PADDING, Font = boldFont,
                Text = iconWidth + " x " + iconHeight };
            iconRectangle = new Rectangle(MIDDLE_LEFT, iconLabel.Bottom, iconWidth, iconHeight);
            this.Controls.Add(iconLabel);
            this.Controls.Add(iconSizeLabel);

            Button changeSize = new Button() { Left = MIDDLE_LEFT - 1, Top = iconRectangle.Bottom + PADDING, AutoSize = true, Text = "Change Icon Size" };
            changeSize.Click += new EventHandler(ChangeSize_Click);
            this.Controls.Add(changeSize);

            if (labelBox.Bottom > changeSize.Bottom)
                separatorY = labelBox.Bottom + PADDING_PANEL;
            else
                separatorY = changeSize.Bottom + PADDING_PANEL;

            Button cancel = new Button() { Left = PADDING, Top = separatorY + PADDING, AutoSize = true, Text = "Cancel" };
            cancel.Click += (sender, e) => { this.Close(); };
            this.Controls.Add(cancel);

            Button add = new Button() { Left = PADDING, Top = separatorY + PADDING, AutoSize = true, Text = "Add" };

            Panel rightPadding = new Panel() { Left = iconRectangle.Right, Width = PADDING_PANEL, Height = 0 };
            Panel bottomPadding = new Panel() { Top = cancel.Bottom - 2, Width = 0, Height = PADDING_PANEL };
            this.Controls.Add(rightPadding);
            this.Controls.Add(bottomPadding);

            this.Paint += new PaintEventHandler(AddShortcutForm_Paint);
            //this.ShowDialog();
        }

        /// <summary>
        /// 
        /// </summary>
        private static void PathBrowse_Click(object sender, System.EventArgs e)
        {
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All Files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        using (myStream)
                        {
                            // Insert code to read the stream here.
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }

        private void ChangeSize_Click(object sender, EventArgs e)
        {
            settingsForm.OpenTab("appearance");
            //ChangeIconSize.ShowDialog(boldFont, shortcutWidth, shortcutHeight);
        }

        private void AddShortcutForm_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(pen, iconRectangle);
            e.Graphics.DrawLine(pen, 0, separatorY, this.Width, separatorY);
        }

        internal class ChangeIconSize
        {
            private static int top2;

            public static void ShowDialog(Font boldFont, int iconWidth, int iconHeight)
            {
                Form changeIconSizeForm = new Form();
                changeIconSizeForm.AutoSize = true;
                changeIconSizeForm.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                changeIconSizeForm.FormBorderStyle = FormBorderStyle.FixedDialog;
                changeIconSizeForm.MinimizeBox = false;
                changeIconSizeForm.MaximizeBox = false;
                changeIconSizeForm.StartPosition = FormStartPosition.CenterScreen;
                changeIconSizeForm.ShowIcon = false;
                changeIconSizeForm.ShowInTaskbar = false;
                changeIconSizeForm.Text = "Change Icon Size";

                Label noteLabel = new Label() { Left = PADDING, Top = PADDING, AutoSize = true, Font = boldFont, Text = "Note: Icon size will be used for ALL launcher shortcuts" };
                top2 = noteLabel.Bottom;
                changeIconSizeForm.Controls.Add(noteLabel);

                Label widthLabel = new Label() { Left = PADDING, Top = top2 + 3, AutoSize = true, Text = "Width" };
                TextBox widthBox = new TextBox() { Left = PADDING + widthLabel.PreferredWidth + 2, Top = top2, Width = 50, SelectionLength = 0, Text = iconWidth.ToString() };
                widthBox.SelectionStart = iconWidth.ToString().Length;
                Label heightLabel = new Label() { Left = widthBox.Right + PADDING_PANEL, Top = top2 + 3, AutoSize = true, Text = "Height" };
                TextBox heightBox = new TextBox() { Left = widthBox.Right + PADDING_PANEL + heightLabel.PreferredWidth + 2, Top = top2, Width = 50, Text = iconHeight.ToString() };
                changeIconSizeForm.Controls.Add(widthLabel);
                changeIconSizeForm.Controls.Add(widthBox);
                changeIconSizeForm.Controls.Add(heightLabel);
                changeIconSizeForm.Controls.Add(heightBox);

                Button cancel = new Button() { Left = PADDING, Top = heightBox.Bottom + PADDING_PANEL * 2, Text = "Cancel" };
                cancel.Click += (sender, e) => { changeIconSizeForm.Close(); };
                changeIconSizeForm.Controls.Add(cancel);

                Button change = new Button() { Left = cancel.Right, Top = heightBox.Bottom + PADDING_PANEL * 2, Text = "Change" };
                change.Click += (sender, e) =>
                {
                    changeIconSizeForm.Close();
                };
                changeIconSizeForm.Controls.Add(change);

                Panel rightPadding = new Panel() { Left = noteLabel.Right, Width = PADDING_PANEL, Height = 0 };
                Panel bottomPadding = new Panel() { Top = cancel.Bottom, Width = 0, Height = PADDING_PANEL };
                changeIconSizeForm.Controls.Add(rightPadding);
                changeIconSizeForm.Controls.Add(bottomPadding);

                changeIconSizeForm.ShowDialog();
                //return (int)inputBox.Value;
            }
        }
    }
}
